package com.android.fastride.manager;

import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;

import java.util.HashMap;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 对话框管理器
 * Created by pengyuantao on 2017/9/28 14:29.
 */

public class DialogManager {

	private static class Holder {
		static final DialogManager instance = new DialogManager();
	}

	public static DialogManager getInstance () {
		return Holder.instance;
	}

	//id计数器
	private int id = 1000;

	//返回一个创建
	private HashMap<Integer, Dialog> dialogMap = new HashMap<Integer, Dialog>();

	/**
	 * 显示一个不能取消的正在加载的对话框
	 */
	public int showNotCancleLoadingDialog (Activity activity, String title, String content) {
		//为了防止Activity销毁的时候对话框没有消失，需要添加一个Fragment
		SweetAlertDialog.Builder builder = new SweetAlertDialog.Builder();
		SweetAlertDialog sweetAlertDialog = builder.type(SweetAlertDialog.PROGRESS_TYPE)
				.context(activity)
				.titleText(title)
				.contentText(content)
				.cancelOnTouchOutside(false)
				.cancelable(false)
				.build();
		final int dialogId = id++;
		dialogMap.put(dialogId, sweetAlertDialog);
		sweetAlertDialog.show();
		return dialogId;
	}

	/**
	 * 显示一个确认对话框
	 */
	public void showConfirmDialog (Activity activity, String title, String content, boolean finishActivity) {
		//为了防止Activity销毁的时候对话框没有消失，需要添加一个Fragment
		SweetAlertDialog.Builder builder = new SweetAlertDialog.Builder();
		SweetAlertDialog sweetAlertDialog = builder.type(SweetAlertDialog.ERROR_TYPE)
				.context(activity)
				.titleText(title)
				.contentText(content)
				.cancelOnTouchOutside(true)
				.cancelable(true)
				.confirmText("确定")
				.confirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sweetAlertDialog) {
						sweetAlertDialog.dismiss();
						if(finishActivity) {
							activity.finish();
						}
					}
				})
				.build();
		sweetAlertDialog.show();
//		return dialogId;
	}

	/**
	 * 显示一个确认对话框
	 */
	public void showSuccessConfirmDialog (Activity activity, String title, String content, boolean finishActivity) {
		//为了防止Activity销毁的时候对话框没有消失，需要添加一个Fragment
		SweetAlertDialog.Builder builder = new SweetAlertDialog.Builder();
		SweetAlertDialog sweetAlertDialog = builder.type(SweetAlertDialog.SUCCESS_TYPE)
				.context(activity)
				.titleText(title)
				.contentText(content)
				.cancelOnTouchOutside(true)
				.cancelable(true)
				.confirmText("确定")
				.confirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
					@Override
					public void onClick(SweetAlertDialog sweetAlertDialog) {
						sweetAlertDialog.dismiss();
						if(finishActivity) {
							activity.finish();
						}
					}
				})
				.build();
		sweetAlertDialog.show();
//		return dialogId;
	}

	/**
	 * 消失对话框
	 *
	 * @param activity
	 * @param id
	 */
	public void dismissDialog (Activity activity, int id) {
		Dialog dialog = dialogMap.get(id);
		dialogMap.remove(id);
		if (dialog != null&&dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	/**
	 * 将Fragment当成一个回调监听使用
	 */
	public static class LifeRecycleFragment extends Fragment {

		private Dialog dialog;

		public LifeRecycleFragment setDialog (Dialog dialog) {
			this.dialog = dialog;
			return this;
		}

		@Override
		public void onDetach () {
			super.onDetach();
			if (dialog.isShowing()) {
				dialog.dismiss();
			}
		}
	}

}
